package com.dhcc.bpm.modules.orga.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.orga.entity.App;
import com.dhcc.bpm.modules.orga.entity.TestApply;
import com.dhcc.bpm.modules.orga.service.IAppApiService;
import com.dhcc.bpm.modules.orga.service.IAppService;
import com.dhcc.bpm.modules.orga.service.ITestApplyService;
import com.dhcc.bpm.modules.orga.vo.AppApiVO;
import com.dhcc.bpm.modules.orga.vo.TestApplyDetailVO;
import com.dhcc.bpm.modules.orga.vo.TestApplyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName TestApplyController
 * @Description 测试申请审批表 控制器
 * @Author daina
 * @Date 2020/2/25 19:25
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-orga/testapply")
@Api(value = "测试申请审批表", tags = "测试申请审批表接口")
public class TestApplyController extends BladeController {

	private ITestApplyService testApplyService;

	private IAppService appService;

	private IAppApiService appApiService;

	/**
	 * @Description 详情
	 * @Author daina
	 * @Date 10:46 2020/2/25
	 * @param testApply
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.orga.vo.TestApplyDetailVO>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入testApply")
	@ApiLog("测试申请审批表 详情")
	@Log(title = "测试申请审批表 详情", businessType = BusinessType.SELECT)
	public R<TestApplyDetailVO> detail(TestApply testApply) {
		TestApply testApplyDetail = testApplyService.getOne(Condition.getQueryWrapper(testApply));
		App app = new App();
		app.setId(testApplyDetail.getAppId());
		App appDetail = appService.getOne(Condition.getQueryWrapper(app));
		List<AppApiVO> appApiList = appApiService.findApiList(appDetail, "2");
		TestApplyDetailVO testApplyDetailVO = new TestApplyDetailVO();
		testApplyDetailVO.setTestApply(testApplyDetail);
		testApplyDetailVO.setApp(appDetail);
		testApplyDetailVO.setApiList(appApiList);
		return R.data(testApplyDetailVO);
	}

	@GetMapping("/testapplyDetail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入testApply")
	@ApiLog("测试申请审批表 详情")
	@Log(title = "测试申请审批表 详情", businessType = BusinessType.SELECT)
	public R<TestApply> testapplyDetail(TestApply testApply) {
		TestApply testApplyDetail = testApplyService.getOne(Condition.getQueryWrapper(testApply));
		return R.data(testApplyDetail);
	}

	@GetMapping("/appPidDetail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入testApply")
	@ApiLog("测试申请审批表 详情")
	@Log(title = "测试申请审批表 详情", businessType = BusinessType.SELECT)
	public R<TestApply> appPidDetail(TestApply testApply) {
		Long id = testApply.getId();
		App byId = appService.getById(id);
        Long appId = byId.getPid();
        TestApply testApplyDetail = testApplyService.selectByAppId(appId);
		return R.data(testApplyDetail);
	}


	/**
	 * @Description 分页 测试申请审批表
	 * @Author daina
	 * @Date 10:45 2020/2/25
	 * @param testApply
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.entity.TestApply>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入testApply")
	@ApiLog("测试申请审批表 分页")
	@Log(title = "测试申请审批表 分页", businessType = BusinessType.SELECT)
	public R<IPage<TestApply>> list(TestApply testApply, Query query) {
		IPage<TestApply> pages = testApplyService.page(Condition.getPage(query), Condition.getQueryWrapper(testApply));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 测试申请审批表
	 * @Author daina
	 * @Date 10:45 2020/2/25
	 * @param testApply
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<TestApplyVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入testApply")
	@ApiLog("测试申请审批表 分页")
	@Log(title = "测试申请审批表 分页", businessType = BusinessType.SELECT)
	public R<IPage<TestApplyVO>> page(TestApplyVO testApply, Query query) {
		IPage<TestApplyVO> pages = testApplyService.selectTestApplyPage(Condition.getPage(query), testApply);
		return R.data(pages);
	}

	/**
	 * @Description 新增 测试申请审批表
	 * @Author daina
	 * @Date 10:45 2020/2/25
	 * @param testApply
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入testApply")
	@ApiLog("测试申请审批表 新增")
	@Log(title = "测试申请审批表 新增", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody TestApply testApply) {
		return R.status(testApplyService.save(testApply));
	}

	/**
	 * @Description 修改 测试申请审批表
	 * @Author daina
	 * @Date 10:45 2020/2/25
	 * @param testApply
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入testApply")
	@ApiLog("测试申请审批表 修改")
	@Log(title = "测试申请审批表 修改", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody TestApply testApply) {
		return R.status(testApplyService.updateById(testApply));
	}

	/**
	 * @Description 新增或修改 测试申请审批表
	 * @Author daina
	 * @Date 10:45 2020/2/25
	 * @param testApply
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入testApply")
	@ApiLog("测试申请审批表 新增或修改")
	@Log(title = "测试申请审批表 新增或修改", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody TestApply testApply) {
		return R.status(testApplyService.saveOrUpdate(testApply));
	}

	
	/**
	 * @Description  删除 测试申请审批表
	 * @Author daina
	 * @Date 10:45 2020/2/25
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("测试申请审批表 删除")
	@Log(title = "测试申请审批表 删除", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(testApplyService.deleteLogic(Func.toLongList(ids)));
	}

	
}
